package com.xrui.hbase.schema;

import com.xrui.hbase.schema.desc.TableLayoutDesc;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;

public final class StaticTableLayoutResolver implements TableLayoutResolver {
    public static final String PROVIDER_NAME = "StaticTableLayoutResolver";

    /**
     * {@inheritDoc}
     */
    @Override
    public synchronized TableLayout resolve(String tableName, Object resource) throws RuntimeException {
        try {
            JsonNode specNode = (JsonNode) resource;
            final ObjectMapper mapper = new ObjectMapper();
            TableLayoutDesc layoutDesc = mapper.readValue(specNode, new TypeReference<TableLayoutDesc>() {
            });
            return TableLayout.newLayout(layoutDesc);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public String getName() {
        return PROVIDER_NAME;
    }
}
